﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Agile_E
{
    public partial class Home : System.Web.UI.Page
    {
        private string empname;
        private string fullname;
        private string dealership;
        private Employees currentEmp =  new Employees();
        private Dealership thisPlace = new Dealership();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (User.Identity.IsAuthenticated)
                {
                    srcgrdpend.SelectCommand = "Select Concat(c.first_name,' ',c.middle_name,' ',c.last_name) as name, c.*,e.*,p.* from Pending_Sales p left outer join Employees e on p.E_ID = e.E_ID " +
                        "left outer join Customers c on p.C_ID = c.C_ID " +
                        "left outer join Vehicles v on p.VIN = v.VIN where v.S_ID = 3";
                    srcgrdsold.SelectCommand = "Select Concat(c.first_name,' ',c.middle_name,' ',c.last_name) as name, c.*,e.*,p.* from Pending_Sales p left outer join Employees e on p.E_ID = e.E_ID " +
                        "left outer join Customers c on p.C_ID = c.C_ID " +
                        "left outer join Vehicles v on p.VIN = v.VIN where v.S_ID = 2";
                    string addr;
                    empname = User.Identity.Name;
                 
                    try
                    {
                        DatabaseHandler.connect();
                        currentEmp = DatabaseHandler.getEmployeeByLogin(empname);
                        dealership = currentEmp.Dealership;
                        string sp = "'";
                        if (dealership.Contains(sp))
                            thisPlace = DatabaseHandler.getDealershipbyID(currentEmp.Dealership_ID);
                        else
                            thisPlace = DatabaseHandler.getDealership(dealership);
                        Session["Employee_ID"] = currentEmp.IDNumber.ToString();
                        Session["Dealership_ID"] = currentEmp.Dealership_ID.ToString();
                        fullname = currentEmp.Info.FirstName + " " + currentEmp.Info.MiddleName + " " + currentEmp.Info.LastName;
                        Session["emp_name"] = fullname;
                        lblemp.Text = fullname;
                        //lblemp.Text = empname;
                        lblname.Text = thisPlace.ContactInformation.FirstName;
                        addr = thisPlace.ContactInformation.Address + ", " + thisPlace.ContactInformation.City + ", " +
                           " " + thisPlace.ContactInformation.State + " " + thisPlace.ContactInformation.Zip;
                        lbladdr.Text = addr;
                        lblphone.Text = thisPlace.ContactInformation.Workphone;
                    }
                    catch (Exception ex)
                    {
                        ((Main)this.Master).writeStatus(ex.Message, true);
                    }
                }
            }
        }
    }
}
